<template>
  <div
    class="article-page"
    v-loading="loading"
    element-loading-text="加载中..."
  >
    <div class="article-title">
      <div class="article-title-left">
        <el-form inline>
          <el-form-item>
            <el-input
              v-model="searchForm.name"
              placeholder="文章标题"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-select v-model="searchForm.status" clearable placeholder="状态">
              <el-option label="有效" :value="1" />
              <el-option label="无效" :value="0" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchFun">查询</el-button>
            <el-button @click="resetFun">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="article-title-right">
        <el-button type="primary" :icon="Plus" @click="addDeptFun('add')">
          添加
        </el-button>
      </div>
    </div>
    <div class="article-table">
      <el-table
        border
        stripe
        :data="tableData"
        :height="store.tableHeight - 40"
      >
        <el-table-column prop="id" label="ID" width="55" align="center" />
        <el-table-column prop="name" label="文章标题" show-overflow-tooltip />
        <el-table-column prop="cate_name" label="文章分类" />
        <el-table-column
          prop="cover"
          label="文章封面"
          width="100"
          align="center"
        >
          <template #default="{ row }">
            <el-image
              :preview-teleported="true"
              style="width: 50px; height: 50px"
              :src="imgSrc(row.cover)"
              :zoom-rate="1.2"
              :preview-src-list="[imgSrc(row.cover)]"
              :initial-index="4"
              fit="cover"
            />
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="90">
          <template #default="{ row }">
            <el-switch
              v-model="row.status"
              active-text="有效"
              :active-value="1"
              inactive-text="无效"
              :inactive-value="0"
              inline-prompt
              @change="statusFun(row)"
            />
          </template>
        </el-table-column>
        <!-- <el-table-column prop="num_like" label="文章点赞数" /> -->
        <el-table-column prop="num_read" label="文章阅读数" />
        <!-- <el-table-column prop="num_collect" label="文章收藏数" /> -->
        <!-- <el-table-column prop="num_comment" label="文章评论数" /> -->
        <el-table-column prop="sort" label="排序" />
        <el-table-column prop="created_at" label="创建时间" width="180" />
        <el-table-column prop="created_user" label="创建人" width="180" />

        <el-table-column label="操作" width="120" align="center">
          <template #default="{ row }">
            <el-button
              type="primary"
              link
              @click="updateDeptFun('update', row)"
            >
              编辑
            </el-button>
            <el-button type="danger" link @click="delFun(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="display: flex; justify-content: center; margin-top: 10px">
        <el-pagination
          v-model:currentPage="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[20, 50, 100, 200]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
  <addDialog ref="addDialogRef" @refreshList="refreshList" />
</template>
<script setup>
import { Plus } from "@element-plus/icons-vue";
import { useCounterStore } from "@/stores/counter.js";
import {
  listApi,
  delApi,
  stateApi,
} from "@/plugins/api/shoppingContent/article";
import addDialog from "./components/DialogContent.vue";
const store = useCounterStore();
let searchForm = ref({});
function searchFun() {
  currentPage.value = 1;
  tableData.value = [];
  getData();
}
function resetFun() {
  searchForm.value = {};
  tableData.value = [];
  currentPage.value = 1;
  getData();
}
let tableData = ref([]);
let loading = ref(false);
const currentPage = ref(1);
const pageSize = ref(20);
const total = ref(0);
const handleSizeChange = (val) => {
  pageSize.value = val;
  selectData.value = [];
  getData();
};
const handleCurrentChange = (val) => {
  currentPage.value = val;
  selectData.value = [];
  getData();
};
function getData() {
  loading.value = true;
  let obj = Object.assign(
    {
      page: currentPage.value,
      pageSize: pageSize.value,
    },
    searchForm.value
  );
  listApi(obj)
    .then((data) => {
      tableData.value = data.list;
      total.value = data.page.total;
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
    });
}
let addDialogRef = ref(null);
function addDeptFun(type) {
  addDialogRef.value.initFun(type);
}
function updateDeptFun(type, row) {
  addDialogRef.value.initFun(type, row);
}
function refreshList() {
  getData();
}
let selectData = ref([]);
function imgSrc(path) {
  return import.meta.env.VITE_APP_BASE_API + path;
}
function delFun(row) {
  ElMessageBox.confirm("确认删除，是否继续?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delApi({
        id: row.id.toString(),
      }).then(() => {
        ElMessage({
          type: "success",
          message: "删除成功！",
        });
        getData();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消。",
      });
    });
}
function statusFun(row) {
  stateApi({
    status: row.status,
    id: row.id,
  }).then(() => {
    ElMessage({
      type: "success",
      message: "修改成功！",
    });
    getData();
  });
}

onMounted(() => {
  getData();
});
</script>
<style scoped lang="scss">
.article-page {
  width: 100%;
  height: 100%;

  .article-title {
    width: 100%;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #eeeeee;
    padding-bottom: 10px;

    .article-title-left {
      font-size: 18px;
      font-weight: 700;
      color: #172b4d;

      :deep(.el-form-item) {
        margin-bottom: 0;
      }
    }
  }

  .article-table {
    padding: 10px;
    box-sizing: border-box;

    .table-img {
      width: 50px;
      height: 50px;
      object-fit: cover;
      cursor: pointer;

      &:hover {
        transform: scale(0.97);
        transition: all 0.3s;
      }
    }
  }
}
</style>
